package ar.com.larreta.reportes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;

import org.apache.log4j.Logger;


public class TXT extends TipoReporte {

    private static final Logger log = Logger.getLogger(TXT.class);
    
    public static final String TXT_CONTENT_TYPE = "plain/text";
    
    public TXT(UtilidadesReportes reportsUtils, String path) {
        super(reportsUtils, path);
    }

    @Override
    public ByteArrayOutputStream getOutputStream(JRDataSource dataSource) throws IOException {
        JasperPrint print = getPrint(dataSource);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            JRTextExporter exporter = new JRTextExporter();
            log.debug("JRTextExporter has been generated.");
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,baos);
            exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,new Float(5.5));
            exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,new Float(14));
            log.debug("Before export report to txt.");
            exporter.exportReport();
            log.debug("After export report to txt.");
        } catch (JRException e) {
            log.error(e);
        }
        return baos;
    }

    @Override
    public String getContentType() {
        return TXT_CONTENT_TYPE;
    }

}
